package com.tinem.platform.service.security.service.impl;

import cn.hutool.core.util.StrUtil;
import com.tinem.platform.module.pojo.co.RedisKeyEnum;
import com.tinem.platform.module.pojo.vo.error.UserException;
import com.tinem.platform.module.pojo.vo.security.verification.code.VerificationRes;
import com.tinem.platform.module.starter.sdk.MessageCode;
import com.tinem.platform.service.security.service.VerificationService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @program: platform
 * @description: ${description}
 * @author: eden#can
 * @Slogan: 没有所谓失败，除非你不再尝试
 * @date: 2021-12-13 11:29
 **/
@Slf4j
@Service
public class VerificationServiceImpl implements VerificationService {

    @Autowired
    StringRedisTemplate redisTemplate;


    /**
     * 验证验证码 img / sms / email
     * @param codeId
     * @param code
     * @return
     */
    @Override
    public VerificationRes verification(String account,String codeId, String code) {

        if(StrUtil.isNotBlank(account)){
            String redisKey = RedisKeyEnum.security_verification_code.getKey(account,codeId);
            return checkVerifiCode(codeId, code, redisKey);
        }
        // 若账号为空，则认为是图片校验
        String redisKey = RedisKeyEnum.security_verification_code.getKey(codeId);
        return checkImgVerifiCode(codeId, code, redisKey);
    }


    /**
     * 校验验证码 (sms / email)
     * @param codeId
     * @param code
     * @param redisKey
     * @return
     */
    private VerificationRes checkVerifiCode(String codeId, String code, String redisKey) {
        String v = redisTemplate.opsForValue().get(redisKey);

        if (v == null) {
            log.error("Mailbox verification code is not found in redis,codeId:{}",codeId);
            throw new UserException(MessageCode.ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE);
        }
        if (!StrUtil.equalsIgnoreCase(code, v)) {
            log.error("Mailbox verification code error");
            throw new UserException(MessageCode.ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE);
        }
        return new VerificationRes(true);
    }

    /**
     * 校验图片验证码 (img)
     * @param codeId
     * @param code
     * @param redisKey
     * @return
     */
    private VerificationRes checkImgVerifiCode(String codeId, String code, String redisKey) {
        String v = redisTemplate.opsForValue().get(redisKey);

        if (v == null) {
            log.error("The graphic verification code is not found in redis,codeId:{}",codeId);
            throw new UserException(MessageCode.THE_GRAPHIC_VERIFICATION_CODE_IS_INCORRECT);
        }
        if (!StrUtil.equalsIgnoreCase(code, v)) {
            log.error("The graphic verification code is incorrect");
            throw new UserException(MessageCode.THE_GRAPHIC_VERIFICATION_CODE_IS_INCORRECT);
        }
        // 删除rediskey，只能验证一次
        redisTemplate.delete(redisKey);

        return new VerificationRes(true);
    }
}
